package net.xuzhen97.distributed.transaction.order.adpter.api;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import net.xuzhen97.distributed.transaction.order.common.Constant;
import net.xuzhen97.distributed.transaction.order.common.util.UserUtil;
import net.xuzhen97.distributed.transaction.order.service.OrderService;
import net.xuzhen97.distributed.transaction.order.service.dto.OrderAddCmd;
import net.xuzhen97.distributed.transaction.order.service.dto.OrderPayCompleteCmd;
import net.xuzhen97.distributed.transaction.order.service.dto.OrderPayCompleteConsistencyCmd;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@Api(tags = "订单")
public class OrderController {

    @Resource
    private OrderService orderService;

    @ApiOperation(
            value = "创建订单"
    )
    @PostMapping("/order")
    @ApiImplicitParam(paramType = "header", name = Constant.HEADER_CURRENT_USER_ID, value = "当前用户Id", dataType = "string", required = true)
    public ResponseEntity<String> addOrder(@RequestBody OrderAddCmd cmd){
        cmd.setOpUserId(UserUtil.getCurrentUserId());
        String id = orderService.addOrder(cmd);
        return ResponseEntity.status(HttpStatus.CREATED).body(id);
    }

    @ApiOperation(
            value = "订单完成支付"
    )
    @PatchMapping("/order/{id}/pay-complete")
    @ApiImplicitParam(paramType = "header", name = Constant.HEADER_CURRENT_USER_ID, value = "当前用户Id", dataType = "string", required = true)
    public ResponseEntity<?> updatePayOrder(@PathVariable String id){

        OrderPayCompleteConsistencyCmd cmd = new OrderPayCompleteConsistencyCmd();
        cmd.setId(id);
        cmd.setOpUserId(UserUtil.getCurrentUserId());
        orderService.updatePayOrderConsistency(cmd);

        return ResponseEntity.status(HttpStatus.CREATED).body(id);
    }
}
